package examples.books

import com.guigarage.flatterfx.FlatterFX
import com.guigarage.flatterfx.FlatterInputType
import de.saxsys.mvvmfx.FluentViewLoader
import de.saxsys.mvvmfx.MvvmFX
import eu.lestard.easydi.EasyDI
import examples.books.backend.LibraryService
import examples.books.backend.LibraryServiceImpl
import examples.books.backend.LibraryServiceMockImpl
import groovy.transform.CompileStatic
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.paint.Color
import javafx.stage.Stage

/**
 * Created by Misty on 2015/6/4.
 */
@CompileStatic
class App extends Application {
    private static final boolean ENABLE_MOCK_SERVICE = true;

    private EasyDI context = new EasyDI();

    public static void main(String[] args) {
        launch(App, args)
    }

    @Override
    void start(Stage primaryStage) throws Exception {
        if (ENABLE_MOCK_SERVICE) {
            context.bindInterface(LibraryService, LibraryServiceMockImpl)
            def libraryServiceMock = context.getInstance(LibraryServiceMockImpl)
            libraryServiceMock.addSomeBooks()
        } else {
            context.bindInterface(LibraryService, LibraryServiceImpl)
        }

        MvvmFX.setCustomDependencyInjector { Class type -> context.getInstance(type) }

        primaryStage.title = "Library JavaFX"
        primaryStage.minWidth = 1200
        primaryStage.maxWidth = 1200
        primaryStage.minHeight = 700

        def scene = new Scene(FluentViewLoader.fxmlView(MainView).load().getView())
        scene.fill = Color.TRANSPARENT
        primaryStage.scene = scene
        primaryStage.show()
        FlatterFX.style(FlatterInputType.DEFAULT)
    }
}
